package com.clouddot.baipai.presenter;


import com.clouddot.baipai.contract.IHomeWorkFragmentContract;
import com.clouddot.baipai.http.entity.response.getCourseByIdResponse;
import com.clouddot.baipai.http.entity.response.getCourseListResponse;

import org.wxy.sdk.base.impl.BasePresenterImpl;

import java.util.ArrayList;

import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class HomeWorkFragmentPresenter extends BasePresenterImpl<IHomeWorkFragmentContract.IView, IHomeWorkFragmentContract.IModel> implements IHomeWorkFragmentContract.IPresenter {
    public HomeWorkFragmentPresenter(IHomeWorkFragmentContract.IView view, IHomeWorkFragmentContract.IModel model) {
        super(view, model);
    }


    @Override
    public void getCourseHomeWorkById(String TeachingClassId, int pageindex, int pagesize) {
        Subscription subscription = mModel.getCourseHomeWorkById("QueryStudentJobPage", TeachingClassId, pageindex, pagesize)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<getCourseByIdResponse>() {
                               @Override
                               public void call(getCourseByIdResponse response) {
                                   mView.success(1, response);
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mView.fail(1, throwable.getMessage());

                            }
                        });
        addSubscription(subscription);
    }

    @Override
    public void getCourseListByStudentId(String StudentId) {
        Subscription subscription = mModel.getCourseListByStudentId("QueryStudentCourse", StudentId)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ArrayList<getCourseListResponse>>() {
                               @Override
                               public void call(ArrayList<getCourseListResponse> response) {
                                   if (!response.isEmpty()) {
                                       mView.success(2, response);
                                   } else {
                                       mView.fail(2, "无课程");

                                   }
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mView.fail(2, throwable.getMessage());

                            }
                        });
        addSubscription(subscription);
    }

    @Override
    public void getCourseListByClassId(String classId) {
        Subscription subscription = mModel.getCourseListByClassId("QueryStudentCourse", classId)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ArrayList<getCourseListResponse>>() {
                               @Override
                               public void call(ArrayList<getCourseListResponse> response) {
                                   if (!response.isEmpty()) {
                                       mView.success(2, response);
                                   } else {
                                       mView.fail(2, "无课程");

                                   }
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mView.fail(2, throwable.getMessage());

                            }
                        });
        addSubscription(subscription);
    }
}
